<?php

class Core_Model_Article_Collection_Content extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('article/content');
        $this->from('{t}article_content', 'a')
             ->field('a.*')
             ->neq('a.status', "'deleted'");
    }

    public function joinAccount() {
        $this->join()->inner('{t}permission_account', 'c')->on('a.mender_id', 'c.id')
                ->closest()
                ->select()->field('c.full_name', 'mender_name');
        return $this;
    }

    public function joinCategory(){
        $this->innerJoin('{t}article_category', 'b', 'b.id', 'a.category_id')
             ->field('b.title', 'category_name');
        return $this;
    }

    public function joinSubject(){
        $this->innerJoin('{t}subject_article','d','d.article_id','a.id')
                    ->field('subject_id')
                    ->field('is_recommend')
                    ->field('d.is_top','subject_is_top');
        return $this;
    }

    public function sortBy($name, $dir) {
        switch ($name) {
            case 'title':
                return $dir == 'asc' ? $this->asc("a.title") : $this->desc("a.title");
            case 'modifiedtime':
                return $dir == 'asc' ? $this->asc("a.modified_time") : $this->desc("a.modified_time");
            case 'hot':
                return $dir == 'asc' ? $this->asc("a.is_hot") : $this->desc("a.is_hot");
            case 'new':
                return $dir == 'asc' ? $this->asc("a.is_new") : $this->desc("a.is_new");
            case 'top':
                return $dir == 'asc' ? $this->asc("a.is_top") : $this->desc("a.is_top");
            case 'clickcount':
                return $dir == 'asc' ? $this->asc("a.click_count") : $this->desc("a.click_count");
            case 'category_id':
                return $dir == 'asc'? $this->asc('a.category_id'): $this->desc('a.category_id');
            case 'isrecommendofsubject':
                return $dir == 'asc'? $this->asc('f.is_recommend'): $this->desc('f.is_recommend');
        }
        return parent::sortBy($name, $dir);
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'id':
                return $this->eq('a.id', ':id:i')->addParameter('id', $arg);
            case 'categoryid':
                return $this->eq('a.category_id', ':category_id:i')->addParameter('category_id', $arg);
            case 'title':
                return $this->like('a.title', ':title:s')->addParameter('title', "%$arg%");
            case 'status':
                return $this->eq('a.status', ':status:s')->addParameter('status', $arg);
            case 'top':
                return $this->eq('a.is_top', $arg);
            case 'new':
                return $this->eq('a.is_new', $arg);
            case 'hot':
                return $this->eq('a.is_hot', $arg);
            case 'categoryname':
                return $this->like('b.title', ':categoryName:s')->addParameter('categoryName', "%$arg%");
            case 'ids':
                return $this->contain('a.id', ':ids:i')->addParameter('ids', $arg);
            case 'creatorid':
                return $this->contain('a.creator_id', ':creatorId:i')->addParameter('creatorId', $arg);
            case 'starttime':
                return $this->lteq('a.start_time', ':startTime:s')->addParameter('startTime', $arg);
            case 'endtime':
                return $this->where()->group()->gteq('a.end_time', ':endTime')->orNext()->isNull('a.end_time')->closest()
                        ->addParameter('endTime', $arg);
            case 'categoryids':
                return $this->contain('a.category_id', ':category_id:i')->addParameter('category_id', $arg);
            case 'subjectid':
                return $this->eq('f.subject_id', ':subject_id:i')->addParameter('subject_id', $arg);
            case 'ownerid':
                return $this->eq('a.owner_id', ':owner_id:i')->addParameter('owner_id', $arg);
            case 'istopofsubject':
                 return $this->eq('f.is_top', ':is_top:i')->addParameter('is_top', $arg);
        }
        return parent::filterBy($name, $arg);
    }

    public function filterByRoleIds($id) {
        $ids = Kd_Util_Array::pack($id);
        $this->where()
                ->inQuery('a.owner_id')
                	->from('{t}permission_account_role')
                	->field('account_id')
                	->contain('role_id', ':roleIds:i')
                	->addParameter('roleIds', $ids);                	
        return $this;
    }

}